package org.quincy.rock.core.dao.sql;

import org.quincy.rock.core.dao.NameInterceptor;
import org.quincy.rock.core.dao.ValueInterceptor;
import org.quincy.rock.core.dao.sql.Sort.Order;

/**
 * <b>AbstractExpression。</b>
 * <p><b>详细说明：</b></p>
 * <!-- 在此添加详细说明 -->
 * 无。
 * 
 * @version 1.0
 * @author wks
 * @since 1.0
 */
public abstract class AbstractExpression implements Expression {

	/**
	 * serialVersionUID。
	 */
	private static final long serialVersionUID = 1L;

	private transient NameInterceptor interceptor;

	@SuppressWarnings("unchecked")
	@Override
	public <T extends Expression> T nameInterceptor(NameInterceptor nameInterceptor) {
		this.interceptor = nameInterceptor;
		return (T) this;
	}

	@Override
	public NameInterceptor nameInterceptor() {
		return interceptor;
	}

	public CharSequence toCharSequence(NameInterceptor nameInterceptor, ValueInterceptor valueInterceptor) {
		return build(new StringBuilder(), nameInterceptor, valueInterceptor);
	}

	public String toString(NameInterceptor nameInterceptor, ValueInterceptor valueInterceptor) {
		return build(new StringBuilder(), nameInterceptor, valueInterceptor).toString();
	}

	@Override
	public String toString() {
		return toString(NameInterceptor.NOOP, (v, dt, match) -> '?');
	}

	@Override
	public int hashCode() {
		return toString().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Order)) {
			return false;
		}
		Order that = (Order) obj;
		return this.toString().equals(that.toString());
	}
}
